package com.scrat.sensorlist;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SensorActivity extends AppCompatActivity {
    public static final String SENSOR_TYPE = "sensor_tye";
    private static final String TAG = "SensorActivity";
    @BindView(R.id.sensor_toolbar)
    Toolbar sensorToolbar;
    @BindView(R.id.tv_data)
    TextView tvData;

    private SensorManager sensorManager;
    private Sensor sensor;
    private MySensorListener mySensorListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        ButterKnife.bind(this);
        sensorToolbar.setTitle("sensor");
        sensorToolbar.setBackgroundResource(R.color.colorPrimary);

        setSupportActionBar(sensorToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        Intent intent = getIntent();
        if (null != intent) {
            int sensorType = intent.getIntExtra(SENSOR_TYPE, -1);
            if (sensorType > -1) {
                sensor = sensorManager.getDefaultSensor(sensorType);
                sensorToolbar.setTitle(sensor.getName());
            }
        }


    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != sensor && null == mySensorListener) {
            mySensorListener = new MySensorListener();
            sensorManager.registerListener(mySensorListener, sensor, SensorManager.SENSOR_DELAY_UI);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != sensor && null != mySensorListener) {
            sensorManager.unregisterListener(mySensorListener);
            mySensorListener = null;
        }
    }


    private class MySensorListener implements SensorEventListener {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            Log.d(TAG, "" + Arrays.toString(values));

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < values.length; i++) {
                stringBuilder.append("values[" + i + "]:" + values[i] + "\n");
            }
            stringBuilder.append("accuracy:" + event.accuracy + "\n");

            tvData.setText(stringBuilder.toString());
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}
